package com.tptech.nearme.rest;

import java.util.List;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.tptech.nearme.helper.GeocodeAction;
import com.tptech.nearme.helper.GeocodeResponse;
import com.tptech.nearme.model.Location;
import com.tptech.nearme.model.User;
import com.tptech.nearme.model.UserLocation;
import com.tptech.nearme.service.ILocationService;
import com.tptech.nearme.service.IUserService;


@Path("/users")
@Produces({MediaType.APPLICATION_JSON})
public class UserResource {

	@Inject
	private IUserService userService;
	
	private ILocationService locationService;

	@POST
	@Path("/register")
	@Consumes({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
	public User registerUser(User user){
		userService.registerUser(user);
		return user;
	}
	
	@GET
	public String check(){
		return "Users are deployed";
	}


	@Path("{userid:[0-9]*}/locations")
	public LocationResource getLocations(@PathParam("userid")Long userid){
		List<UserLocation> locationList = userService.getUserLocations(userid);
		return new LocationResource(locationList);
	}
	
	@GET
	@Path("{userid:[0-9]*}/location/add")
	//?userid={userid:[0-9]*}&desc={desc}&address={address}&country={country}
	public Response registerLocation(
			@PathParam("userid")String username
//			,
//			@QueryParam("country")String country,
//			@QueryParam("address")String address,
//			@QueryParam("desc")String description
			){
		try {
//			GeocodeResponse response = GeocodeAction.geocode(address,country);
//			Long userid = Long.parseLong(username);
//			Location location = locationService.findSimilarLocation(response.getLocation());
//			
//			UserLocation userLoc = new UserLocation();
//			userLoc.setAddress(address);
//			userLoc.setLat(response.getLat());
//			
//			userLoc.setLon(response.getLon());
//			userLoc.setLocation(location);
//			userLoc.setDescription(description);
//			
//			userService.registerLocation(userid, 
//					userLoc);
			return Response.ok(username).build();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
}
